#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007, pysozluk-qt Development Team
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.
#

import sys
from PyQt4 import QtCore, QtGui
from about import *
from configure import *
from pysozlukglobals import version, updateServer

class TrayBase(QtGui.QWidget):
	def __init__(self, *args):
		apply(QtGui.QWidget.__init__,(self, ) + args)

		self.settings = QtCore.QSettings()
		self.updateCheck = self.settings.value("checkboxes/update").toBool();

		self.tray = QtGui.QSystemTrayIcon(self)
		self.trayMenu = QtGui.QMenu()
		self.action_translate = QtGui.QAction(QtGui.QIcon("images/forward.png"), u'Translate', self)
		self.action_sozluk = QtGui.QAction(QtGui.QIcon("images/start.png"), u'Open Gui', self)
		self.action_configuration = QtGui.QAction(QtGui.QIcon("images/configure.png"), u'Configuration', self)
		self.action_about = QtGui.QAction(QtGui.QIcon("images/about.png"), u'About', self)
		self.action_quit = QtGui.QAction(QtGui.QIcon("images/quit.png"), u'Quit', self)

		self.trayMenu.addAction(self.action_translate)
		self.trayMenu.addSeparator()
		self.trayMenu.addAction(self.action_sozluk)
		self.trayMenu.addSeparator()
		self.trayMenu.addAction(self.action_configuration)
		self.trayMenu.addAction(self.action_about)
		self.trayMenu.addAction(self.action_quit)
		
		self.connect(self.action_translate, QtCore.SIGNAL("triggered()"), self.translate)
		self.connect(self.action_sozluk, QtCore.SIGNAL("triggered()"), self.sozlukOpen)
		self.connect(self.action_configuration, QtCore.SIGNAL("triggered()"), self.sozlukConfiguration)
		self.connect(self.action_about, QtCore.SIGNAL("triggered()"), self.sozlukAbout)
		self.connect(self.action_quit, QtCore.SIGNAL("triggered()"), self.sozlukQuit)
		
		self.trayIcon = QtGui.QIcon("images/pyqtsozluktray.png")
		self.tray.setContextMenu(self.trayMenu)
		self.tray.setIcon(self.trayIcon)
		self.tray.setToolTip(u"PyQtSözlük")
		self.tray.show()
		
		if(self.updateCheck):
			import urllib2
			try:
				response = urllib2.urlopen(updateServer + 'latest.txt')
				latestversion = response.read()
				if(latestversion > version and self.settings.value("popupmessages").toBool()):
					self.tray.showMessage(u"PyQtSözlük", u"There's a higher version of PyQtSözlük available", QtGui.QSystemTrayIcon.Information, 8000)
			except:
				if(self.settings.value("popupmessages").toBool()):
					self.tray.showMessage(u"PyQtSözlük", u"An error accured while connecting to server", QtGui.QSystemTrayIcon.Information, 8000)

	def translate(self):
		
		cb = QtGui.QApplication.clipboard()
		text = cb.text(cb.Selection)
		text = text.toLower()
		translater = "/usr/bin/turkceyecevir"
		proc = QtCore.QProcess(self)
		proc.start(translater)

	def sozlukOpen(self):
		p = "/usr/bin/pysozluk-qt"
		proc = QtCore.QProcess(self)
		proc.start(p)

	def sozlukConfiguration(self):
		self.configuresozluk = ConfigureBase(self)

	def sozlukAbout(self):
		self.aboutsozluk = AboutBase(self)

	def sozlukQuit(self):
		print u"Quit"
		sys.exit()

if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	
	app.setOrganizationName("pysozluk-qt")
	app.setApplicationName("pysozluk-qt")
	
	if( QtGui.QSystemTrayIcon.isSystemTrayAvailable() != 1 ):
		message = QtGui.QMessageBox(self)
		message.setText(u'Could not detect system tray')
		message.setWindowTitle(u'PyQtSözlük')
		message.setIcon(QtGui.QMessageBox.Warning)
		message.addButton("Quit", QtGui.QMessageBox.RejectRole)
		message.exec_()
		response = message.clickedButton().text()
		
		if response == "Quit":
			sys.exit()

	else:
		print u"System Tray available, application starting.."
		settings = QtCore.QSettings()
		settings.setValue("popupmessages", QtCore.QVariant(QtGui.QSystemTrayIcon.supportsMessages()));
	
		app.setQuitOnLastWindowClosed(0)
		myob = TrayBase()
			
	sys.exit(app.exec_())